package com.springboot.config;

import com.springboot.common.servelt.TestServlet;
import org.springframework.boot.autoconfigure.web.embedded.TomcatWebServerFactoryCustomizer;
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryCustomizer;
import org.springframework.boot.autoconfigure.web.servlet.TomcatServletWebServerFactoryCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: TongRui乀
 * @Date: 2020/3/15 13:05
 * @description：
 */
@Configuration
public class TomcatConfig {

//    @Bean  不生效
    TomcatServletWebServerFactory tomcatServletWebServerFactory(){

        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();

        factory.setPort(8090);

        return factory;
    }

    @Bean
    WebServerFactoryCustomizer<TomcatServletWebServerFactory> webServerFactoryCustomizer(){

        return factory -> {
            TomcatServletWebServerFactory tomcatFactory = (TomcatServletWebServerFactory) factory;

            tomcatFactory.setPort(8090);

        };

    }


    @Bean
    public ServletRegistrationBean servletRegistrationBean(){

        ServletRegistrationBean<TestServlet> registrationBean = new ServletRegistrationBean<>(new TestServlet());

        registrationBean.addUrlMappings("/**");

        return registrationBean;
    }

}
